package com.cbw.controller;

import com.cbw.bean.BootStrapTableExpress;
import com.cbw.bean.Express;
import com.cbw.bean.Message;
import com.cbw.bean.ResultData;
import com.cbw.mvc.ResponseBody;
import com.cbw.service.ExpressService;
import com.cbw.util.DateFormatUtil;
import com.cbw.util.JSONUtil;
import com.cbw.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {
    /**
     * 控制台显示数据
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = ExpressService.console();
        Message msg = new Message();//消息对象
        if (data.size()==0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);//将消息对象转成json
        System.out.println("控制台数据:"+json);
        return json;  //该方法在DispatchServlet的service中被执行并获取到返回的结果json，然后返回给ajax
    }

    /**
     * 分页列表
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        System.out.println("分页查询开始");
        //获取参数:分页查询的起始索引、每页记录数
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //开始分页查询
        List<Express> list = ExpressService.findAll(true, offset, pageNumber);
        //修改入库出库时间格式，list中Express的出入库时间都是TimeStamp类型Mar 31, 2021, 2:25:41 AM，都改成string类型，另外快递状态也改成string.
        //即新建一个实体类BootStrapTableExpress,存放分页后的数据
        ArrayList<BootStrapTableExpress> list1 = new ArrayList<>();
        for (Express e : list) {
            String inTime = DateFormatUtil.format(e.getInTime());//将TimeStamp类型转成string
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status=e.getStatus()==0?"待取件":"已取件";
            String code=e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list1.add(e2);
        }
        System.out.println("分页查询结果:"+list1);
        //将集合封装为bootstrap-table识别的格式
        ResultData<BootStrapTableExpress> data = new ResultData<>();//创建分页对象(list,total)
        data.setRows(list1); //将当前页查询的数据集以及总数量存入分页对象中
        data.setTotal(ExpressService.console().get(0).get("data1_size"));
        String json = JSONUtil.toJSON(data);//将分页对象转成json
        System.out.println("快递分页结果"+json);
        return json; //传到DispatchServlet中
        //接口测试:http://localhost:8080/express/list.do?offset=0&pageNumber=2
    }

    /**
     * 快递录入
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/express/add.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始录入快递");
        //接收参数
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userphone = request.getParameter("userphone");
        Express e = new Express(number,username,userphone,company, UserUtil.getUserPhone(request.getSession()));
        boolean flag = ExpressService.insert(e);//执行插入
        System.out.println(flag);
        Message msg = new Message();//返回给前台的消息对象
        if (flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("快递录入成功");
        }else {
            //录入失败
            msg.setStatus(-1);
            msg.setResult("快递录入失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    /**
     * 根据单号查询快递
     * @param req
     * @param resp
     * @return
     */
    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest req,HttpServletResponse resp){
        //接收参数
        String number = req.getParameter("number");
        Express e = ExpressService.findByNumber(number);
        System.out.println("查询到的快递:"+e);
        Message msg = new Message();//消息对象，存放服务器给前台的数据
        if (e==null){  //查询空
            msg.setStatus(-1);
            msg.setResult("单号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
        //测试:http://localhost:8080/express/find.do?number=123123123
        //{"status":0,"result":"查询成功","data":{"id":10,"number":"123123123","username":"李四","userPhone":"123456789","company":"顺丰快递","code":"819821","inTime":"Apr 2, 2021, 12:38:47 AM","status":0,"sysPhone":"123456789"}}
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req,HttpServletResponse resp){
        System.out.println("开始更新");
        //接收参数
        int id = Integer.parseInt(req.getParameter("id"));
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userphone = req.getParameter("userphone");
        int status = Integer.parseInt(req.getParameter("status"));
        //封装参数
        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setUsername(username);
        newExpress.setUserPhone(userphone);
        newExpress.setStatus(status);
        //调用service更新操作
        boolean flag = ExpressService.update(id, newExpress);
        //填充消息对象
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        //消息对象转成Json，传递到DispatchServlet
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req,HttpServletResponse resp){
        //接收参数
        int id = Integer.parseInt(req.getParameter("id"));
        //调用service删除操作
        boolean flag = ExpressService.delete(id);
        //填充消息对象
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        //json处理
        String json = JSONUtil.toJSON(msg);
        //返回到DispatchServlet
        return json;
    }


}
